package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.mapper.OrdersMapper;
import com.itheima.reggie.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private AddressBookService addressBookService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    @Override
    @Transactional
    public Boolean submit(Orders orders) {
        //获取默认地址 将默认地址的值 传进去
        AddressBook defaultAddressBook=addressBookService.getDefaultAddressBook(orders.getUserId());
        BeanUtils.copyProperties(defaultAddressBook, orders);
        //设置收货人
        orders.setConsignee(defaultAddressBook.getConsignee());
        orders.setUserName(defaultAddressBook.getConsignee());
        //设置下单地址
        orders.setAddress(defaultAddressBook.getDetail());
        //设置订单号
        orders.setNumber(UUID.randomUUID().toString());
        //初始化订单状态 代付款
        orders.setStatus(2);
        //设置下单时间
        orders.setOrderTime(LocalDateTime.now());
        //付款时间
        orders.setCheckoutTime(LocalDateTime.now());
        //获取当前购物车内容
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper=new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId, orders.getUserId());
        List<ShoppingCart> shoppingCartList=shoppingCartService.list(shoppingCartLambdaQueryWrapper);
        //初始化 付款金额
        Long amount = 0L;

        List<OrderDetail> orderDetails = new ArrayList<>();
        for (ShoppingCart s : shoppingCartList) {
//            BeanUtils.copyProperties(orders, s);
            OrderDetail orderDetail=new OrderDetail();
            //订单明细 绑定订单号
            orderDetail.setOrderId(orders.getId());
            BigDecimal price = BigDecimal.valueOf(0L);
            //计算付款金额
            //根据菜品Id 获取价格
            if(s.getDishId() != null){
                Dish dish=dishService.getById(s.getDishId());
                price = dish.getPrice();
            }
            //根据套餐Id 获取价格
            if(s.getSetmealId() != null){
                Setmeal setmeal=setmealService.getById(s.getSetmealId());
                price = setmeal.getPrice();
            }
            //当前菜品价格*数量=当前订单明细 总价格
            Long tempPrice = price.longValue() * s.getNumber();
            BeanUtils.copyProperties(s, orderDetail);
            //清空填充错误的ID
            orderDetail.setId(null);
            //初始化 付款金额
            orderDetail.setAmount(BigDecimal.valueOf(tempPrice));
            //总菜品价格
            amount += tempPrice;
            orderDetails.add(orderDetail);
        }

        orders.setAmount(BigDecimal.valueOf(amount));

        orders.setId(null);
        //保存订单信息 返回主键id
        this.save(orders);

        for (OrderDetail orderDetail : orderDetails) {
            orderDetail.setOrderId(orders.getId());
            //    //保存订单明细
           orderDetailService.save(orderDetail);
        }
        //清空该用户的购物车信息
        shoppingCartService.remove(shoppingCartLambdaQueryWrapper);
        return true;
    }
}
